package com.fanghaoran.dao;

import com.fanghaoran.bean.Interaction;
import com.fanghaoran.entity.StudyInfo;
import com.fanghaoran.view.user.studyPlanFrame.StudyPlanPanel;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: win11
 * @Date: 2023/03/24/14:38
 * @Description: 学习信息的数据库处理
 */
public class StudyInfoDao {
    /**
     * 更新学习时长
     */
    public void updateStudyTime(long studyTime, int userId) throws SQLException {
        String sql = "update study_info set studyTime=? where userId=?;";
        BaseDao.executeUpdate(sql, studyTime, userId);
    }

    /**
     * 获取已学习时长
     */
    public Interaction getStudyTime(int userId) throws Exception {
        String sql = "select studyTime from study_info where userId=?;";
        List<StudyInfo> studyInfos = BaseDao.executeQuery(StudyInfo.class, sql, userId);
        if (studyInfos.isEmpty()) {
            return new Interaction(null, 0);
        } else {
            return new Interaction(null, studyInfos.get(0).getStudyTime());
        }

    }


    //学习计划

    /**
     * 获取用户的学习计划
     */
    public Interaction getStudyPlan(int userId) throws Exception {
        String sql = "select studyPlan from study_info where userId=?;";
        List<StudyInfo> studyInfos = BaseDao.executeQuery(StudyInfo.class, sql, userId);
        if (studyInfos.isEmpty()) {
            return new Interaction(null, 0);
        } else {
            return new Interaction(null, studyInfos.get(0).getStudyPlan());
        }
    }

    /**
     * 更新用户的学习计划
     *
     * @param userId 用户id->更新哪个用户的学习数据
     */
    public void updateStudyPlan(int userId) throws SQLException {
        String sql = "update study_info set studyPlan=? where userId=?;";
        BaseDao.executeUpdate(sql, StudyPlanPanel.plan.getText(), userId);
    }


    //已学单词数

    /**
     * 更新数据库中存储的已学单词个数
     */
    public void updateWordAmount(int userId) throws Exception {
        String sql = "update study_info set wordAccount=? where userId=?;";
        BaseDao.executeUpdate(sql, StudyPlanPanel.wordAmount, userId);
    }

    /**
     * 获取数据库中存储的该用户的已学单词个数
     */
    public Interaction getWordAmount(int userId) throws Exception {
        String sql = "select wordAccount from study_info where userId=?";
        List<StudyInfo> studyInfos = BaseDao.executeQuery(StudyInfo.class, sql, userId);
        if (studyInfos.isEmpty()) {
            return new Interaction(null, 0);
        } else {
            return new Interaction(null, studyInfos.get(0).getWordAccount());
        }
    }
}
